package com.sjzd.meeting.controller.admin;

import com.github.pagehelper.PageInfo;
import com.sjzd.meeting.pojo.Role;
import com.sjzd.meeting.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
@RequestMapping("/admin")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    /**
     *  查询所有角色
     * @param pageNum
     * @param model
     * @return
     */
    @GetMapping("/roles")
    public String findAll(@RequestParam(value = "pageNum",defaultValue = "1") String pageNum,
                            Model model){
        PageInfo<Role> pageInfo = roleService.findAll(Integer.parseInt(pageNum), 3);
        model.addAttribute("pageInfo", pageInfo);
        return "admin/roles";
    }

    /**
     *  根据条件进行角色查询
     * @param status
     * @param pageNum
     * @param model
     * @return
     */
    @PostMapping("/roles/condition")
    public String findByCondition(@RequestParam(name="rname", defaultValue = "") String rname,
                                  @RequestParam(name = "status") Integer status,
                                  @RequestParam(name = "pageNum") String pageNum,
                          Model model){
        PageInfo<Role> pageInfo = roleService.findByCondition(rname, status, Integer.parseInt(pageNum), 3);
        model.addAttribute("pageInfo", pageInfo);
        return "admin/roles :: role_table";
    }

    /**
     *  跳转至角色更改页面
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/roles/update")
    public String update(Integer id, Model model){
        Role role = roleService.findById(id);
        model.addAttribute("role", role);
        return "admin/role-input";
    }

    /**
     *  执行角色更改的数据库操作
     * @param role
     * @param redirectAttributes
     * @return
     */
    @PostMapping("/roles/update")
    public String update(Role role,  RedirectAttributes redirectAttributes){
        int flag = roleService.update(role);
        if (flag > 0) {
            redirectAttributes.addFlashAttribute("message", "更新成功！");
        } else {
            redirectAttributes.addFlashAttribute("message", "更新失败！");
        }
        return "redirect:/admin/roles";
    }

    /**
     *  跳转至角色新增页面
     * @return
     */
    @GetMapping("/roles/input")
    public String add(Model model) {
        model.addAttribute("role", new Role());
        return "admin/role-input";
    }


    /**
     *  新增角色的数据库操作
     * @param role
     * @param redirectAttributes
     * @return
     */
    @PostMapping("/roles/save")
    public String add(Role role, RedirectAttributes redirectAttributes) {
        int flag = roleService.save(role);
        if (flag > 0) {
            redirectAttributes.addFlashAttribute("message", "新增成功！");
        } else {
            redirectAttributes.addFlashAttribute("message", "新增失败！");
        }

        return "redirect:/admin/roles";
    }


    /**
     *  根据角色id进行删除操作
     * @param id
     * @param redirectAttributes
     * @return
     */
    @GetMapping("/roles/delete")
    public String delete(Integer id, RedirectAttributes redirectAttributes) {
        int i = roleService.deleteById(id);
        if (i < 0) {
            redirectAttributes.addFlashAttribute("message", "删除失败！");
        } else {
            redirectAttributes.addFlashAttribute("message", "删除成功！");
        }
        return "redirect:/admin/roles";
    }
}
